<!DOCTYPE html>
<meta charset="utf-8" />
<title>Popover beforetoggle event opening new popovers</title>
<link rel="author" href="mailto:masonf@chromium.org">
<link rel=help href="https://html.spec.whatwg.org/multipage/indices.html#event-beforetoggle">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/popover-utils.js"></script>

<div popover id=p1>Popover 1
  <div popover id=p2>Popover 2
    <div popover id=p3>Popover 3</div>
  </div>
</div>
<div id=outside>Outside</div>
<dialog id=dialog>Dialog</dialog>

<script>
  function getSignal(t) {
    const controller = new AbortController();
    t.add_cleanup(() => controller.abort());
    return controller.signal;
  }

  test((t) => {
    p1.showPopover();
    p1.addEventListener('beforetoggle',() => p2.showPopover(),{signal: getSignal(t)});
    p1.hidePopover(); // Potential crash here
    assert_false(p1.matches(':popover-open'));
    assert_false(p2.matches(':popover-open'));
  },'Open popover from closing beforetoggle event');

  test((t) => {
    p1.showPopover();
    p1.addEventListener('beforetoggle',() => p2.showPopover(),{signal: getSignal(t)});
    p2.addEventListener('beforetoggle',() => p3.showPopover(),{signal: getSignal(t)});
    p1.hidePopover(); // Potential crash here
    assert_false(p1.matches(':popover-open'));
    assert_false(p2.matches(':popover-open'));
    assert_false(p3.matches(':popover-open'));
  },'Open double-nested popovers from closing beforetoggle event');

  test(t => {
    p1.showPopover();
    p1.addEventListener('beforetoggle',() => p2.showPopover(),{signal: getSignal(t)});
    p2.addEventListener('beforetoggle',() => p3.showPopover(),{signal: getSignal(t)});
    dialog.showModal(); // Potential crash here
    assert_false(p1.matches(':popover-open'));
    assert_false(p2.matches(':popover-open'));
    assert_false(p3.matches(':popover-open'));
    dialog.close();
  },'Open double-nested popovers from closing beforetoggle event, dialog open');

  promise_test(async t => {
    p1.showPopover();
    p1.addEventListener('beforetoggle',() => p2.showPopover(),{signal: getSignal(t)});
    p2.addEventListener('beforetoggle',() => p3.showPopover(),{signal: getSignal(t)});
    await clickOn(outside); // Potential crash here
    assert_false(p1.matches(':popover-open'));
    assert_false(p2.matches(':popover-open'));
    assert_false(p3.matches(':popover-open'));
  },'Open double-nested popovers from closing beforetoggle event, light dismiss');
</script>
